<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="统筹区编码" prop="aaa027">
              <el-input v-model="queryParams.aaa027" placeholder="请输入统筹区编码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="单位编码" prop="dw01">
              <el-input v-model="queryParams.dw01" placeholder="请输入单位编码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="单位名称" prop="dw02">
              <el-input v-model="queryParams.dw02" placeholder="请输入单位名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="个人帐号" prop="gr03">
              <el-input v-model="queryParams.gr03" placeholder="请输入个人帐号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="姓名" prop="gr04">
              <el-input v-model="queryParams.gr04" placeholder="请输入姓名" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="状态，1：已缴" prop="grmx01">
              <el-input v-model="queryParams.grmx01" placeholder="请输入状态，1：已缴" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="缴费月度" prop="grmx02">
              <el-input v-model="queryParams.grmx02" placeholder="请输入缴费月度" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="缴费时间" prop="grmx03">
              <el-date-picker clearable
                v-model="queryParams.grmx03"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择缴费时间"
              />
            </el-form-item>
            <el-form-item label="上年工资" prop="gr18">
              <el-input v-model="queryParams.gr18" placeholder="请输入上年工资" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="个人缴费基数" prop="gr19">
              <el-input v-model="queryParams.gr19" placeholder="请输入个人缴费基数" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="个人缴费额" prop="gr20">
              <el-input v-model="queryParams.gr20" placeholder="请输入个人缴费额" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="单位划转额" prop="grmx04">
              <el-input v-model="queryParams.grmx04" placeholder="请输入单位划转额" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="单位代缴额" prop="grmx05">
              <el-input v-model="queryParams.grmx05" placeholder="请输入单位代缴额" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="grmx06">
              <el-input v-model="queryParams.grmx06" placeholder="请输入" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="单位平均工资" prop="dw14">
              <el-input v-model="queryParams.dw14" placeholder="请输入单位平均工资" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="单位缴费比例" prop="cs03">
              <el-input v-model="queryParams.cs03" placeholder="请输入单位缴费比例" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="单位基数算法" prop="dw68">
              <el-input v-model="queryParams.dw68" placeholder="请输入单位基数算法" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="操作员代码" prop="dw24">
              <el-input v-model="queryParams.dw24" placeholder="请输入操作员代码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="系统时间" prop="systime">
              <el-date-picker clearable
                v-model="queryParams.systime"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择系统时间"
              />
            </el-form-item>
            <el-form-item label="计息年度" prop="grjx01">
              <el-input v-model="queryParams.grjx01" placeholder="请输入计息年度" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="tz01">
              <el-input v-model="queryParams.tz01" placeholder="请输入" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="dh">
              <el-input v-model="queryParams.dh" placeholder="请输入" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="dw91">
              <el-input v-model="queryParams.dw91" placeholder="请输入" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="文件地址" prop="fileurl">
              <el-input v-model="queryParams.fileurl" placeholder="请输入文件地址" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['yuanshidian:ab04new:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['yuanshidian:ab04new:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['yuanshidian:ab04new:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['yuanshidian:ab04new:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="ab04newList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="主键ID" align="center" prop="id" v-if="true" />
        <el-table-column label="统筹区编码" align="center" prop="aaa027" />
        <el-table-column label="单位编码" align="center" prop="dw01" />
        <el-table-column label="单位名称" align="center" prop="dw02" />
        <el-table-column label="个人帐号" align="center" prop="gr03" />
        <el-table-column label="姓名" align="center" prop="gr04" />
        <el-table-column label="状态，1：已缴" align="center" prop="grmx01" />
        <el-table-column label="缴费月度" align="center" prop="grmx02" />
        <el-table-column label="缴费时间" align="center" prop="grmx03" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.grmx03, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="上年工资" align="center" prop="gr18" />
        <el-table-column label="个人缴费基数" align="center" prop="gr19" />
        <el-table-column label="个人缴费额" align="center" prop="gr20" />
        <el-table-column label="单位划转额" align="center" prop="grmx04" />
        <el-table-column label="单位代缴额" align="center" prop="grmx05" />
        <el-table-column label="" align="center" prop="grmx06" />
        <el-table-column label="单位平均工资" align="center" prop="dw14" />
        <el-table-column label="单位缴费比例" align="center" prop="cs03" />
        <el-table-column label="单位基数算法" align="center" prop="dw68" />
        <el-table-column label="操作员代码" align="center" prop="dw24" />
        <el-table-column label="系统时间" align="center" prop="systime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.systime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="计息年度" align="center" prop="grjx01" />
        <el-table-column label="" align="center" prop="tz01" />
        <el-table-column label="" align="center" prop="dh" />
        <el-table-column label="" align="center" prop="dw91" />
        <el-table-column label="数据状态:1待提交2审核中3已审核4驳回" align="center" prop="status" />
        <el-table-column label="删除状态:0未删除，1已删除" align="center" prop="delStatus" />
        <el-table-column label="文件地址" align="center" prop="fileurl" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['yuanshidian:ab04new:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['yuanshidian:ab04new:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改新人员账户明细(缴费明细)对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="ab04newFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="统筹区编码" prop="aaa027">
          <el-input v-model="form.aaa027" placeholder="请输入统筹区编码" />
        </el-form-item>
        <el-form-item label="单位编码" prop="dw01">
          <el-input v-model="form.dw01" placeholder="请输入单位编码" />
        </el-form-item>
        <el-form-item label="单位名称" prop="dw02">
          <el-input v-model="form.dw02" placeholder="请输入单位名称" />
        </el-form-item>
        <el-form-item label="个人帐号" prop="gr03">
          <el-input v-model="form.gr03" placeholder="请输入个人帐号" />
        </el-form-item>
        <el-form-item label="姓名" prop="gr04">
          <el-input v-model="form.gr04" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="状态，1：已缴" prop="grmx01">
          <el-input v-model="form.grmx01" placeholder="请输入状态，1：已缴" />
        </el-form-item>
        <el-form-item label="缴费月度" prop="grmx02">
          <el-input v-model="form.grmx02" placeholder="请输入缴费月度" />
        </el-form-item>
        <el-form-item label="缴费时间" prop="grmx03">
          <el-date-picker clearable
            v-model="form.grmx03"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择缴费时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="上年工资" prop="gr18">
          <el-input v-model="form.gr18" placeholder="请输入上年工资" />
        </el-form-item>
        <el-form-item label="个人缴费基数" prop="gr19">
          <el-input v-model="form.gr19" placeholder="请输入个人缴费基数" />
        </el-form-item>
        <el-form-item label="个人缴费额" prop="gr20">
          <el-input v-model="form.gr20" placeholder="请输入个人缴费额" />
        </el-form-item>
        <el-form-item label="单位划转额" prop="grmx04">
          <el-input v-model="form.grmx04" placeholder="请输入单位划转额" />
        </el-form-item>
        <el-form-item label="单位代缴额" prop="grmx05">
          <el-input v-model="form.grmx05" placeholder="请输入单位代缴额" />
        </el-form-item>
        <el-form-item label="" prop="grmx06">
          <el-input v-model="form.grmx06" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="单位平均工资" prop="dw14">
          <el-input v-model="form.dw14" placeholder="请输入单位平均工资" />
        </el-form-item>
        <el-form-item label="单位缴费比例" prop="cs03">
          <el-input v-model="form.cs03" placeholder="请输入单位缴费比例" />
        </el-form-item>
        <el-form-item label="单位基数算法" prop="dw68">
          <el-input v-model="form.dw68" placeholder="请输入单位基数算法" />
        </el-form-item>
        <el-form-item label="操作员代码" prop="dw24">
          <el-input v-model="form.dw24" placeholder="请输入操作员代码" />
        </el-form-item>
        <el-form-item label="系统时间" prop="systime">
          <el-date-picker clearable
            v-model="form.systime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择系统时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计息年度" prop="grjx01">
          <el-input v-model="form.grjx01" placeholder="请输入计息年度" />
        </el-form-item>
        <el-form-item label="" prop="tz01">
          <el-input v-model="form.tz01" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="dh">
          <el-input v-model="form.dh" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="dw91">
          <el-input v-model="form.dw91" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="文件地址" prop="fileurl">
          <el-input v-model="form.fileurl" placeholder="请输入文件地址" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Ab04new" lang="ts">
import { listAb04new, getAb04new, delAb04new, addAb04new, updateAb04new } from '@/api/yuanshidian/ab04new';
import { Ab04newVO, Ab04newQuery, Ab04newForm } from '@/api/yuanshidian/ab04new/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const ab04newList = ref<Ab04newVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const ab04newFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: Ab04newForm = {
  id: undefined,
  aaa027: undefined,
  dw01: undefined,
  dw02: undefined,
  gr03: undefined,
  gr04: undefined,
  grmx01: undefined,
  grmx02: undefined,
  grmx03: undefined,
  gr18: undefined,
  gr19: undefined,
  gr20: undefined,
  grmx04: undefined,
  grmx05: undefined,
  grmx06: undefined,
  dw14: undefined,
  cs03: undefined,
  dw68: undefined,
  dw24: undefined,
  systime: undefined,
  grjx01: undefined,
  tz01: undefined,
  dh: undefined,
  dw91: undefined,
  status: undefined,
  delStatus: undefined,
  fileurl: undefined,
}
const data = reactive<PageData<Ab04newForm, Ab04newQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    aaa027: undefined,
    dw01: undefined,
    dw02: undefined,
    gr03: undefined,
    gr04: undefined,
    grmx01: undefined,
    grmx02: undefined,
    grmx03: undefined,
    gr18: undefined,
    gr19: undefined,
    gr20: undefined,
    grmx04: undefined,
    grmx05: undefined,
    grmx06: undefined,
    dw14: undefined,
    cs03: undefined,
    dw68: undefined,
    dw24: undefined,
    systime: undefined,
    grjx01: undefined,
    tz01: undefined,
    dh: undefined,
    dw91: undefined,
    status: undefined,
    delStatus: undefined,
    fileurl: undefined,
    params: {
    }
  },
  rules: {
    id: [
      { required: true, message: "主键ID不能为空", trigger: "blur" }
    ],
    aaa027: [
      { required: true, message: "统筹区编码不能为空", trigger: "blur" }
    ],
    dw01: [
      { required: true, message: "单位编码不能为空", trigger: "blur" }
    ],
    dw02: [
      { required: true, message: "单位名称不能为空", trigger: "blur" }
    ],
    gr03: [
      { required: true, message: "个人帐号不能为空", trigger: "blur" }
    ],
    gr04: [
      { required: true, message: "姓名不能为空", trigger: "blur" }
    ],
    grmx01: [
      { required: true, message: "状态，1：已缴不能为空", trigger: "blur" }
    ],
    grmx02: [
      { required: true, message: "缴费月度不能为空", trigger: "blur" }
    ],
    grmx03: [
      { required: true, message: "缴费时间不能为空", trigger: "blur" }
    ],
    gr18: [
      { required: true, message: "上年工资不能为空", trigger: "blur" }
    ],
    gr19: [
      { required: true, message: "个人缴费基数不能为空", trigger: "blur" }
    ],
    gr20: [
      { required: true, message: "个人缴费额不能为空", trigger: "blur" }
    ],
    grmx04: [
      { required: true, message: "单位划转额不能为空", trigger: "blur" }
    ],
    grmx05: [
      { required: true, message: "单位代缴额不能为空", trigger: "blur" }
    ],
    grmx06: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    dw14: [
      { required: true, message: "单位平均工资不能为空", trigger: "blur" }
    ],
    cs03: [
      { required: true, message: "单位缴费比例不能为空", trigger: "blur" }
    ],
    dw68: [
      { required: true, message: "单位基数算法不能为空", trigger: "blur" }
    ],
    dw24: [
      { required: true, message: "操作员代码不能为空", trigger: "blur" }
    ],
    systime: [
      { required: true, message: "系统时间不能为空", trigger: "blur" }
    ],
    grjx01: [
      { required: true, message: "计息年度不能为空", trigger: "blur" }
    ],
    tz01: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    dh: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    dw91: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "数据状态:1待提交2审核中3已审核4驳回不能为空", trigger: "change" }
    ],
    delStatus: [
      { required: true, message: "删除状态:0未删除，1已删除不能为空", trigger: "change" }
    ],
    fileurl: [
      { required: true, message: "文件地址不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询新人员账户明细(缴费明细)列表 */
const getList = async () => {
  loading.value = true;
  const res = await listAb04new(queryParams.value);
  ab04newList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  ab04newFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: Ab04newVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加新人员账户明细(缴费明细)";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: Ab04newVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getAb04new(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改新人员账户明细(缴费明细)";
}

/** 提交按钮 */
const submitForm = () => {
  ab04newFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateAb04new(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addAb04new(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: Ab04newVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除新人员账户明细(缴费明细)编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delAb04new(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('yuanshidian/ab04new/export', {
    ...queryParams.value
  }, `ab04new_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
